//
//  WBBaseViewController.swift
//  Weibo
//
//  Created by wade on 17/2/17.
//  Copyright © 2017年 wade. All rights reserved.
//

import UIKit

class WBBaseViewController: UIViewController {

    var isLogon = false
    
    var vistorInfoDic : [String:String]?
    
    var tableview : UITableView?
    
    var refreshControl : UIRefreshControl?
    
    var isPullup = false
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
        WBNetworkManager.shared.userLogon ? loadData() : ()
        
        NotificationCenter.default.addObserver(self, selector: #selector(loginSuccess), name:NSNotification.Name(rawValue: WBUserLoginSuccessNotification), object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func loadData() {
        refreshControl?.endRefreshing()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

}

extension WBBaseViewController{

    func loginSuccess(n:Notification) {
        
        print("baseview 登陆成功")
        
        view = nil
        
        NotificationCenter.default.removeObserver(self)
    }
    
    func loginAction() {
        print("login")
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: WBUserShouldLoginNotification), object: nil)
        
    }
    
    func registerAction() {
        print("register")
    }
    
}

extension WBBaseViewController{

    func setupUI() -> () {
        view.backgroundColor = UIColor.white
        
        WBNetworkManager.shared.userLogon ? setupTableVIew() : setupVisitorView()
    }
    
    
    func setupTableVIew() {
        
        tableview = UITableView.init(frame: view.bounds, style: .plain)
        
        tableview?.delegate = self
        tableview?.dataSource = self
        
        view.addSubview(tableview!)
        
        tableview?.scrollIndicatorInsets = tableview!.contentInset
        
        refreshControl = UIRefreshControl()
        
        tableview?.addSubview(refreshControl!)
        
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        
        
    }

    func setupVisitorView() {
        
        let visitorView = WBVisitorView(frame: view.bounds)
        
        view.addSubview(visitorView)
        
        visitorView.visitorInfo = vistorInfoDic
        
        visitorView.loginButton.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        
        visitorView.registerButton.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
    }
}

extension WBBaseViewController:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let section = tableView.numberOfSections - 1
        
        if row < 0 || section < 0 {
        
            return
        
        }
        
        let count = tableView.numberOfRows(inSection: section)
        
        if row == (count - 1) && !isPullup 
        {
            print("上拉刷新")
            
            isPullup = true
            
            loadData()
        }
        
    }

}
